home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Technology Seed / ADC Seed CD - July 1999.toast / Carbon SDK 1.0d10c3 / Sample Code / AppearanceSample / CDEFTester.cp < prev    next >
Encoding:
Text File  |  1999-05-01  |  13.6 KB  |  639 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        CDEFTester.cp
  3.  
  4.     Contains:    Code to demonstrate all types of controls.
  5.  
  6.     Version:    Appearance 1.0 SDK
  7.  
  8.     Copyright:    © 1997-1999 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Edward Voas
  13.  
  14.         Other Contact:        7 of 9, Borg Collective
  15.  
  16.         Technology:            OS Technologies Group
  17.  
  18.     Writers:
  19.  
  20.         (jss)    Jeff Shulman
  21.         (MAA)    Matt Ackeret
  22.         (edv)    Ed Voas
  23.  
  24.     Change History (most recent first):
  25.  
  26.        <1.8>      4/7/99    jss        Fix CarbonLib compilation problems
  27.          <8>     2/18/98    MAA        remove protos for menu bar accessors
  28.          <7>    11/21/97    MAA        Add UserPane
  29.          <6>     11/7/97    MAA        Add tab Boolean support in DisplayPartCode
  30.          <5>     11/5/97    MAA        add Tabs
  31.          <4>    10/29/97    MAA        Make Activate and Deactivate enable and disable global font
  32.                                     menu, respectively
  33.          <3>    10/28/97    MAA        Add EditText/other controls/ChangeControlFont back in
  34.          <2>    10/28/97    edv        Use RadioGroup control!
  35.          <1>     9/11/97    edv        First checked in.
  36. */
  37.  
  38. //
  39. //    This file opens a dialog and adds a menu to the menu bar which allows the user
  40. //    to create and play with the different control types.
  41. //
  42.  
  43. #include "AppearanceSamplePrefix.h"
  44.  
  45. #include <Appearance.h>
  46. #include <ControlDefinitions.h>
  47. #include <Dialogs.h>
  48. #include <TextUtils.h>
  49. #include "CDEFTester.h"
  50. #include "CDEFTesterUtils.h"
  51.  
  52. extern MenuHandle        gFontMenu;        // Menu used to choose a font
  53.  
  54. enum
  55. {
  56.     kBevelButton        = 1,
  57.     kChasingArrows        = 2,
  58.     kDisclosureTriangle    = 3,
  59.     kDivider            = 4,
  60.     kEditText            = 5,
  61.     kFinderHeader        = 6,
  62.     kGroupBox            = 7,
  63.     kIconCDEF            = 8,
  64.     kImageWell            = 9,
  65.     kLittleArrows        = 10,
  66.     kPictureCDEF        = 11,
  67.     kPlacard            = 12,
  68.     kPopupArrow            = 13,
  69.     kProgressBar        = 14,
  70.     kScrollBar            = 15,
  71.     kStaticText            = 16,
  72.     kTabs                = 17,
  73.     kUserPane            = 18,
  74.     kPushButton            = 19,
  75.     kCheckBox            = 20,
  76.     kRadioButton        = 21,
  77.     kSlider                = 22,
  78.     kClock                = 23,
  79.     kList                = 24
  80. };
  81.  
  82. enum
  83. {
  84.     kValueText            = 2,
  85.     kValueOK            = 3,
  86.     kValueCancel        = 4
  87. };
  88.  
  89. #define Height( r )        ( (r).bottom - (r).top )
  90. #define Width( r )        ( (r).right - (r).left )
  91.  
  92. MenuHandle CDEFTester::fMenu = nil;
  93.  
  94.  
  95. CDEFTester::CDEFTester() : BaseWindow( 130 )
  96. {
  97.     ControlRef        control;
  98.     Rect            bounds;
  99.     CGrafPtr        port;
  100.  
  101.     if (fMenu == nil)
  102.         fMenu = GetMenu(133);
  103.         
  104.     fControl = nil;
  105.     fIsTab = false;
  106.  
  107.     port = GetWindowPort( fWindow );
  108.     GetPortBounds( port, &fPartRect );
  109.     
  110.     fPartRect.top = fPartRect.bottom - 20;
  111.     fPartRect.right = fPartRect.left + 260;
  112.     OffsetRect( &fPartRect, 5, -5 );
  113.     
  114.     CreateRootControl( fWindow, &control );
  115.     
  116.     bounds = fPartRect;
  117.     bounds.right = bounds.left + 60;
  118.     
  119.     OffsetRect( &bounds, 275, 0 );
  120.     CreatePushButtonControl( fWindow, &bounds, "\pDisable", &fDisableButton );
  121.  
  122.     bounds.right = bounds.left + 70;
  123.     OffsetRect( &bounds, 70, 0 );
  124.     CreatePushButtonControl( fWindow, &bounds, "\pSet Value", &fSetValueButton );
  125.  
  126.     DeactivateControl( fDisableButton );
  127.     DeactivateControl( fSetValueButton );
  128. }
  129.  
  130. CDEFTester::~CDEFTester()
  131. {
  132.     MenuHandle theMenu;
  133.     theMenu = GetMyMenu();
  134.     
  135.     if (theMenu)
  136.     {
  137.          DeleteMenu( GetMenuID( theMenu ) );
  138.          InvalMenuBar();
  139.     }
  140.  
  141.     if ( fControl ) 
  142.         DisposeControl( fControl );
  143.  
  144.     if (gFontMenu)
  145.     {
  146.         DisableMenuItem(gFontMenu, 0);
  147.         InvalMenuBar();
  148.     }
  149. }
  150.  
  151. void
  152. CDEFTester::Activate( EventRecord& event )
  153. {
  154.     ControlRef        root;
  155.     
  156.     BaseWindow::Activate( event );
  157.     
  158.     if ( GetRootControl( fWindow, &root ) == noErr )
  159.         ActivateControl( root );
  160.  
  161.     if ((gFontMenu) && (fControl))
  162.     {
  163.         EnableMenuItem(gFontMenu, 0);
  164.         InvalMenuBar();
  165.     }
  166. }
  167.  
  168. void
  169. CDEFTester::Deactivate( EventRecord& event )
  170. {
  171.     ControlRef        root;
  172.     
  173.     BaseWindow::Deactivate( event );
  174.     
  175.     if ( GetRootControl( fWindow, &root ) == noErr )
  176.         DeactivateControl( root );
  177.         
  178.     if (gFontMenu)
  179.     {
  180.         DisableMenuItem(gFontMenu, 0);
  181.         InvalMenuBar();
  182.     }
  183. }
  184.  
  185. void
  186. CDEFTester::Draw()
  187. {
  188.     DrawControls( fWindow );
  189. }
  190.  
  191. void
  192. CDEFTester::HandleClick( EventRecord& event )
  193. {
  194.     Point        where = event.where;
  195.     ControlRef    control;
  196.     SInt16        part;
  197.     
  198.     SetPort( GetWindowPort( fWindow ) );
  199.     GlobalToLocal( &where );
  200.     
  201.     part = FindControl( where, fWindow, &control );
  202.     
  203.     if ( control == fDisableButton )
  204.     {
  205.         if ( TrackControl( control, where, (ControlActionUPP)-1L ) )
  206.         {
  207.             if ( GetControlReference( control ) == 0 )
  208.             {
  209.                 DeactivateControl( fControl );
  210.                 SetControlTitle( control, "\pEnable" );
  211.                 SetControlReference( control, 1 );
  212.             }
  213.             else
  214.             {
  215.                 ActivateControl( fControl );
  216.                 SetControlTitle( control, "\pDisable" );
  217.                 SetControlReference( control, 0 );
  218.             }
  219.         }
  220.     }
  221.     else if ( control == fSetValueButton )
  222.     {
  223.         if ( TrackControl( control, where, (ControlActionUPP)-1L ) )
  224.         {
  225.             SetValue();
  226.         }
  227.     }
  228.     else
  229.     {
  230.         DisplayPartCode( part );
  231.         
  232.         if ( part )
  233.         {
  234.             part = TrackControl( control, where, (ControlActionUPP)-1L );
  235.             DisplayPartCode( part );
  236.         }
  237.     }
  238. }
  239.  
  240.  
  241.  
  242. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  243. //    Ä HandleKeyDown
  244. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  245. //    Call HandleControlKey to give the key to the control
  246. //
  247.  
  248. void CDEFTester::HandleKeyDown(EventRecord &event)
  249. {
  250.     HandleControlKey(this->fControl, (event.message & keyCodeMask)>>16, event.message & charCodeMask, event.modifiers);
  251. }
  252.  
  253.  
  254. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  255. //    Ä Idle
  256. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  257. //    Call IdleControls to give time to controls like edit text, chasing arrows,
  258. //    and the indeterminate progress indicator.
  259. //
  260. void
  261. CDEFTester::Idle()
  262. {
  263.     IdleControls( fWindow );
  264. }
  265.  
  266. void CDEFTester::ChangeControlFont(SInt16 menuID, SInt16 itemNo)
  267. {
  268.     ControlFontStyleRec fontStyleRec;
  269.     MenuHandle theMenu = GetMenuHandle(menuID);
  270.     SInt16 fontID;
  271.     OSErr theErr;
  272.     Rect bounds;
  273.     SInt16            baseLine;
  274.  
  275.     if (fControl)
  276.     {
  277.         GetMenuItemFontID(theMenu, itemNo, &fontID);    
  278.         fontStyleRec.flags = kControlUseFontMask;
  279.         fontStyleRec.font = fontID;
  280.         theErr = SetControlFontStyle(fControl, &fontStyleRec);
  281.  
  282.         HideControl(fControl); // the rect is going to change.. want to erase old rect..
  283.         // reset the font bounds since we changed the size..
  284.         if ( GetBestControlRect( fControl, &bounds, &baseLine ) == noErr )
  285.             SetControlBounds( fControl, &bounds );
  286.  
  287.         ShowControl(fControl);
  288.     }
  289. }
  290.  
  291.     
  292.  
  293. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  294. //    Ä HandleMenuSelection
  295. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  296. //    Create the right type of control. Ignore any menu items not from our menu.
  297. //
  298. void
  299. CDEFTester::HandleMenuSelection( SInt16 menuID, SInt16 itemNo )
  300. {
  301.     ControlRef        newControl = nil;
  302.     
  303.     if ((menuID != GetMenuID( GetMyMenu() )) && (menuID != kMenuFonts)) return;
  304.     
  305.     if (menuID == kMenuFonts)
  306.     {
  307.         ChangeControlFont(menuID, itemNo);
  308.         return;
  309.     }
  310.     
  311.     fIsTab = false;
  312.     
  313.     switch ( itemNo )
  314.     {
  315.         case kTabs:
  316.             newControl = CreateTabs( fWindow );
  317.             if (newControl)
  318.                 fIsTab = true;
  319.             break;
  320.             
  321.         case kUserPane:
  322.             newControl = CreateUserPane( fWindow );
  323.             break;
  324.             
  325.           case kStaticText:
  326.               newControl = CreateStaticText( fWindow );
  327.               break;
  328.  
  329.           case kEditText:
  330.               newControl = CreateEditText( fWindow );
  331.               break;
  332.  
  333.         case kPushButton:
  334.             newControl = CreatePushButton( fWindow );
  335.             break;
  336.         
  337.         case kCheckBox:
  338.             newControl = CreateCheckBox( fWindow );
  339.             break;
  340.         
  341.         case kSlider:
  342.             newControl = CreateSlider( fWindow );
  343.             break;
  344.         
  345.         case kClock:
  346.             newControl = CreateClock( fWindow );
  347.             break;
  348.         
  349.         case kRadioButton:
  350.             newControl = CreateRadioButton( fWindow );
  351.             break;
  352.             
  353.         case kBevelButton:
  354.             newControl = CreateBevelButton( fWindow );
  355.             break;
  356.  
  357.         case kChasingArrows:
  358.             newControl = CreateChasingArrows( fWindow );
  359.             break;
  360.  
  361.         case kDivider:
  362.             newControl = CreateDivider( fWindow );
  363.             break;
  364.  
  365.         case kDisclosureTriangle:
  366.             newControl = CreateTriangle( fWindow );
  367.             break;
  368.  
  369.         case kFinderHeader:
  370.             newControl = CreateFinderHeader( fWindow );
  371.             break;
  372.  
  373.         case kIconCDEF:
  374.             newControl = CreateIconCDEF( fWindow );
  375.             break;
  376.  
  377.         case kPictureCDEF:
  378.             newControl = CreatePictureCDEF( fWindow );
  379.             break;
  380.  
  381.         case kProgressBar:
  382.             newControl = CreateProgressBar( fWindow );
  383.             break;
  384.  
  385.         case kLittleArrows:
  386.             newControl = CreateLittleArrows( fWindow );
  387.             break;
  388.  
  389.         case kGroupBox:
  390.             newControl = CreateGroupBox( fWindow );
  391.             break;
  392.  
  393.         case kPlacard:
  394.             newControl = CreatePlacard( fWindow );
  395.             break;
  396.  
  397.         case kPopupArrow:
  398.             newControl = CreatePopupArrow( fWindow );
  399.             break;
  400.  
  401.         case kScrollBar:
  402.             newControl = CreateScrollBar( fWindow );
  403.             break;
  404.  
  405.         case kImageWell:
  406.             newControl = CreateImageWell( fWindow );
  407.             break;
  408.  
  409.         case kList:
  410.             newControl = CreateList( fWindow );
  411.             break;
  412.     }
  413.  
  414.     if ( newControl )
  415.     {
  416.         Rect bounds;
  417.         SInt16            baseLine;
  418.     
  419.         if ( fControl ) DisposeControl( fControl );
  420.         fControl = newControl;
  421.         if ( GetBestControlRect( fControl, &bounds, &baseLine ) == noErr )
  422.             SetControlBounds( fControl, &bounds );
  423.         
  424.         CenterControlInWindow();
  425.         ShowControl( fControl );
  426.         
  427.         SetControlTitle( fDisableButton, "\pDisable" );
  428.         SetControlReference( fDisableButton, 0 );
  429.         ActivateControl( fDisableButton );
  430.         ActivateControl( fSetValueButton );
  431.         
  432.         //now the WYSIWYG menu can be used
  433.         if (gFontMenu)
  434.         {
  435.             EnableMenuItem(gFontMenu, 0);
  436.             InvalMenuBar();
  437.         }
  438.     }
  439.     else if ( fControl == nil )
  440.     {
  441.         SetControlTitle( fDisableButton, "\pDisable" );
  442.         SetControlReference( fDisableButton, 0 );
  443.         DeactivateControl( fDisableButton );
  444.         DeactivateControl( fSetValueButton );
  445.     }
  446. }
  447.  
  448. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  449. //    Ä CenterControlInWindow
  450. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  451. //    Moves the control into the center of the window.
  452. //
  453. void
  454. CDEFTester::CenterControlInWindow()
  455. {
  456.     Rect        bounds;
  457.     SInt16        left, top, height, width;
  458.     CGrafPtr    port;
  459.     Rect        portRect;
  460.  
  461.     if ( fControl == nil ) return;
  462.     
  463.     GetControlBounds( fControl, &bounds );
  464.     
  465.     height = Height( bounds );
  466.     width = Width( bounds );
  467.  
  468.     port = GetWindowPort( fWindow );
  469.     GetPortBounds( port, &portRect );
  470.     
  471.     left = (( Width( portRect ) - width ) / 2 ) + portRect.left;
  472.     top = (( Height( portRect ) - height ) / 2 ) + portRect.top;
  473.     
  474.     SetRect( &bounds, left, top, left + width, top + height );
  475.     SetControlBounds( fControl, &bounds );
  476. }
  477.  
  478. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  479. //    Ä SetValue
  480. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  481. //    Allows the user to choose a specific value for the control.
  482. //
  483. void
  484. CDEFTester::SetValue()
  485. {
  486.     DialogPtr        dialog;
  487.     SInt16            itemNo;
  488.     Str255            text;
  489.     SInt32            number;
  490.     ControlKeyFilterUPP    filterProc;
  491.     ControlHandle    control;
  492.     
  493.     dialog = GetNewDialog( 2004, nil, (WindowRef)-1L );
  494.     if ( dialog == nil ) return;
  495.     
  496.     SetDialogDefaultItem( dialog, kValueOK );
  497.     SetDialogCancelItem( dialog, kValueCancel );
  498.     
  499.     // Set the text item to the current control value
  500.     GetDialogItemAsControl( dialog, kValueText, &control );
  501.     NumToString( GetControlValue( fControl ), text );
  502.     SetDialogItemText( (Handle)control, text );
  503.     
  504.     // make sure its selected
  505.     SelectDialogItemText( dialog, kValueText, 0, 32767 );
  506.     
  507.     // add our simple numbers-only key filter
  508.     filterProc = NewControlKeyFilterProc( NumericFilter );
  509.     SetControlData( control, 0, kControlEditTextKeyFilterTag, sizeof( filterProc ),
  510.                     (Ptr)&filterProc );
  511.  
  512.     itemNo = 0;
  513.     while( itemNo != kValueCancel && itemNo != kValueOK )
  514.     {
  515.         ModalDialog( nil, &itemNo );
  516.     }
  517.     DisposeControlKeyFilterUPP( filterProc );
  518.     
  519.     if ( itemNo == kValueCancel )
  520.     {
  521.         DisposeDialog( dialog );
  522.         return;
  523.     }
  524.         
  525.     // Get the text
  526.     GetDialogItemAsControl( dialog, kValueText, &control );
  527.     GetDialogItemText( (Handle)control, text );
  528.     StringToNum( text, &number );
  529.     
  530.     DisposeDialog( dialog );
  531.     
  532.     SetControlValue( fControl, number );
  533. }
  534.  
  535. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  536. //    Ä DisplayPartCode
  537. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  538. //    Prints the constant for the part code passed in.
  539. //
  540. void
  541. CDEFTester::DisplayPartCode( SInt16 part )
  542. {
  543.     Str255        lastString;
  544.     
  545.     SetPort( GetWindowPort( fWindow ) );
  546.     
  547.     EraseRect( &fPartRect );
  548.     MoveTo( fPartRect.left, fPartRect.bottom - 4 );
  549.     
  550.     DrawString( "\pLast Part: " );
  551.     NumToString( part, lastString );
  552.     DrawString( lastString );
  553.     
  554.     if (!fIsTab) 
  555.     // Tabs return part codes corresponding to the new tab you're pushing on/selecting.  The part numbers
  556.     // don't represent the standard part numbers as shown below.
  557.     {
  558.         DrawString( "\p (" );
  559.         switch ( part )
  560.         {
  561.             case kControlNoPart:
  562.                 DrawString( "\pkControlNoPart" );
  563.                 break;
  564.                 
  565.             case kControlClockPart:
  566.                 DrawString( "\pkControlClockPart" );
  567.                 break;
  568.                 
  569.             case kControlLabelPart:
  570.                 DrawString( "\pkControlLabelPart" );
  571.                 break;
  572.     
  573.             case kControlMenuPart:
  574.                 DrawString( "\pkControlMenuPart" );
  575.                 break;
  576.     
  577.             case kControlEditTextPart:
  578.                 DrawString( "\pkControlEditTextPart" );
  579.                 break;
  580.     
  581.             case kControlIconPart:
  582.                 DrawString( "\pkControlIconPart" );
  583.                 break;
  584.     
  585.             case kControlPicturePart:
  586.                 DrawString( "\pkControlPicturePart" );
  587.                 break;
  588.     
  589.             case kControlTrianglePart:
  590.                 DrawString( "\pkControlTrianglePart" );
  591.                 break;
  592.     
  593.             case kControlButtonPart:
  594.                 DrawString( "\pkControlButtonPart" );
  595.                 break;
  596.     
  597.             case kControlCheckBoxPart:
  598.                 DrawString( "\pkControlCheckBoxPart" );
  599.                 break;
  600.     
  601.             case kControlUpButtonPart:
  602.                 DrawString( "\pkControlUpButtonPart" );
  603.                 break;
  604.     
  605.             case kControlDownButtonPart:
  606.                 DrawString( "\pkControlDownButtonPart" );
  607.                 break;
  608.     
  609.             case kControlPageUpPart:
  610.                 DrawString( "\pkControlPageUpPart" );
  611.                 break;
  612.     
  613.             case kControlPageDownPart:
  614.                 DrawString( "\pkControlPageDownPart" );
  615.                 break;
  616.     
  617.             case kControlIndicatorPart:
  618.                 DrawString( "\pkControlIndicatorPart" );
  619.                 break;
  620.     
  621.             case kControlDisabledPart:
  622.                 DrawString( "\pkControlInactivePart" );
  623.                 break;
  624.     
  625.             case kControlInactivePart:
  626.                 DrawString( "\pkControlInactivePart" );
  627.                 break;
  628.     
  629.         }
  630.         DrawString( "\p)" );
  631.     }
  632. }
  633.  
  634. MenuHandle CDEFTester::GetMyMenu(void)
  635. {
  636.     return(fMenu);
  637. }
  638.  
  639.